package org.firebug.spring.boot.datasource.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "org.firebug.spring.boot.datasource.repository.mapper0", sqlSessionFactoryRef = "sqlSessionFactory0")
public class DataSource0Config {
    @Bean(name = "dataSource0")
    @ConfigurationProperties(prefix = "spring.datasource.test0")
    public DataSource dateSource0() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "sqlSessionFactory0")
    public SqlSessionFactory sqlSessionFactory0(@Qualifier("dataSource0") DataSource datasource0) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource0);
        //        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/**.xml"));
        return bean.getObject();
    }

    @Bean("sqlSessionTemplate0")
    public SqlSessionTemplate sqlsessiontemplate(@Qualifier("sqlSessionFactory0") SqlSessionFactory sqlSessionFactory0) {
        return new SqlSessionTemplate(sqlSessionFactory0);
    }
}
